package vip.xiaonuo.biz.modular.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import vip.xiaonuo.biz.modular.event.entity.BizEvent;
import vip.xiaonuo.biz.modular.event.entity.BizEventConfig;
import vip.xiaonuo.biz.modular.event.mapper.BizEventConfigMapper;
import vip.xiaonuo.biz.modular.event.mapper.BizEventMapper;
import vip.xiaonuo.biz.modular.event.service.BizEventGoodsCheckService;
import vip.xiaonuo.biz.modular.event.service.BizEventReplaceService;
import vip.xiaonuo.biz.modular.goods.entity.BizGoods;
import vip.xiaonuo.biz.modular.goods.entity.BizGoodsLedger;
import vip.xiaonuo.biz.modular.goods.entity.BizGoodsLedgerCheck;
import vip.xiaonuo.biz.modular.goods.mapper.BizGoodsLedgerCheckMapper;
import vip.xiaonuo.biz.modular.goods.mapper.BizGoodsLedgerMapper;
import vip.xiaonuo.biz.modular.goods.mapper.BizGoodsMapper;
import vip.xiaonuo.biz.modular.user.entity.BizUser;
import vip.xiaonuo.biz.modular.user.mapper.BizUserMapper;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class BizEventGoodsCheckServiceImpl implements BizEventGoodsCheckService {

    @Resource
    private BizGoodsLedgerMapper bizGoodsLedgerMapper;

    @Resource
    private BizGoodsLedgerCheckMapper bizGoodsLedgerCheckMapper;

    @Resource
    private BizGoodsMapper bizGoodsMapper;

    @Resource
    private BizEventReplaceService bizEventReplaceService;

    @Resource
    private BizEventConfigMapper bizEventConfigMapper;

    @Resource
    private BizEventMapper bizEventMapper;

    @Resource
    private BizUserMapper bizUserMapper;

    @Override
    public void handleCheckForNorm(String ledgerId, Integer checkType) {
        BizGoodsLedger bizGoodsLedger = bizGoodsLedgerMapper.selectById(ledgerId);
        BizGoods goods = bizGoodsMapper.selectById(bizGoodsLedger.getGoodsId());
        String goodsName = bizEventReplaceService.getGoodsNameForEntity(goods);
        String userId = bizGoodsLedger.getDeliveryUserId();
        String userName = bizEventReplaceService.getUserName(userId);
        //所有检查项
        List<BizGoodsLedgerCheck> goodsLedgerCheckList = bizGoodsLedgerCheckMapper.getListByLedgerIdAndCheckType(ledgerId, checkType);
        if (!CollectionUtils.isEmpty(goodsLedgerCheckList)) {
            //未完成数量
            int nfNum = 0;
            //合格数量
            int qualifyNum = 0;
            for (BizGoodsLedgerCheck ledgerCheck : goodsLedgerCheckList) {
                if (null == ledgerCheck.getCheckResult()) {
                    nfNum++;
                } else {
                    if (ledgerCheck.getCheckResult() == 1) {
                        qualifyNum++;
                    }
                }
            }
            if (checkType == 0) {
                this.addRecordForNfBorCheck(ledgerId, goodsName, userName, nfNum, userId, goods);
                this.addRecordForPrBorCheck(ledgerId, goodsName, userName, qualifyNum, goodsLedgerCheckList.size(), userId, goods);
            }
            if (checkType == 1) {
                this.addRecordForNfRetCheck(ledgerId, goodsName, userName, nfNum, userId, goods);
                this.addRecordForPrRetCheck(ledgerId, goodsName, userName, qualifyNum, goodsLedgerCheckList.size(), userId, goods);
            }
        }
    }

    /**
     * 添加记录-未完成借用检查
     *
     * @param ledgerId
     * @param goodsName
     * @param userName
     * @param nfNum
     * @param userId
     * @param goods
     */
    private void addRecordForNfBorCheck(String ledgerId, String goodsName, String userName, int nfNum, String userId, BizGoods goods) {
        if (nfNum > 0) {
            //查询对应配置
            BizEventConfig eventConfig = bizEventConfigMapper.selectById("27");
            //事件描述
            String eventDesc = eventConfig.getEventDesc();
            eventDesc = eventDesc.replace("{{goods}}", goodsName);
            eventDesc = eventDesc.replace("{{borrower}}", userName);
            eventDesc = eventDesc.replace("{{chk-inc}}", nfNum + "");
            //操作人
            //组合实体类
            BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
            event.setEventConfigId(eventConfig.getId());
            event.setEventTime(DateUtil.date());
            event.setEventDesc(eventDesc);
            event.setEventUserId(userId);
            event.setEventAction(7);
            event.setGoodsId(goods.getId());
            event.setGoodsLedgerId(ledgerId);
            event.setCabinetGroupId(goods.getBioCabinetGroupId());
            event.setCabinetId(goods.getBioCabinetId());
            event.setCabinetDoorId(goods.getBioCabinetDoorId());
            bizEventReplaceService.clearBizEvent(event);
            bizEventMapper.insert(event);

            //【邮件】本次刷卡开门的卡主，柜门管理员
            bizEventReplaceService.sendEmailForUser(userId,event);
            bizEventReplaceService.sendEmailForDoorAdmin(event);
        }
    }

    /**
     * 添加记录-未完成归还检查
     *
     * @param ledgerId
     * @param goodsName
     * @param userName
     * @param nfNum
     * @param userId
     * @param goods
     */
    private void addRecordForNfRetCheck(String ledgerId, String goodsName, String userName, int nfNum, String userId, BizGoods goods) {
        if (nfNum > 0) {
            //查询对应配置
            BizEventConfig eventConfig = bizEventConfigMapper.selectById("28");
            //事件描述
            String eventDesc = eventConfig.getEventDesc();
            eventDesc = eventDesc.replace("{{goods}}", goodsName);
            eventDesc = eventDesc.replace("{{borrower}}", userName);
            eventDesc = eventDesc.replace("{{chk-inc}}", nfNum + "");
            //操作人
            //组合实体类
            BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
            event.setEventConfigId(eventConfig.getId());
            event.setEventTime(DateUtil.date());
            event.setEventDesc(eventDesc);
            event.setEventUserId(userId);
            event.setEventAction(8);
            event.setGoodsId(goods.getId());
            event.setGoodsLedgerId(ledgerId);
            event.setCabinetGroupId(goods.getBioCabinetGroupId());
            event.setCabinetId(goods.getBioCabinetId());
            event.setCabinetDoorId(goods.getBioCabinetDoorId());
            bizEventReplaceService.clearBizEvent(event);
            bizEventMapper.insert(event);

            //【邮件】本次刷卡开门的卡主，柜门管理员
            bizEventReplaceService.sendEmailForUser(userId,event);
            bizEventReplaceService.sendEmailForDoorAdmin(event);
        }

    }

    /**
     * 添加记录-借用检查有不合格项
     *
     * @param ledgerId
     * @param goodsName
     * @param userName
     * @param qualifyNum
     * @param totalNum
     * @param userId
     * @param goods
     */
    private void addRecordForPrBorCheck(String ledgerId, String goodsName, String userName, int qualifyNum, int totalNum, String userId, BizGoods goods) {
        if (qualifyNum != totalNum) {
            //查询对应配置
            BizEventConfig eventConfig = bizEventConfigMapper.selectById("29");
            //事件描述
            String eventDesc = eventConfig.getEventDesc();
            eventDesc = eventDesc.replace("{{goods}}", goodsName);
            eventDesc = eventDesc.replace("{{borrower}}", userName);
            //合格率 合格数/总数
            String rate = NumberUtil.roundStr(NumberUtil.mul(NumberUtil.div(qualifyNum, totalNum), 100), 2);
            eventDesc = eventDesc.replace("{{pass-rate}}", rate + "%");
            //组合实体类
            BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
            event.setEventConfigId(eventConfig.getId());
            event.setEventTime(DateUtil.date());
            event.setEventDesc(eventDesc);
            event.setEventUserId(userId);
            event.setEventAction(7);
            event.setGoodsId(goods.getId());
            event.setGoodsLedgerId(ledgerId);
            event.setCabinetGroupId(goods.getBioCabinetGroupId());
            event.setCabinetId(goods.getBioCabinetId());
            event.setCabinetDoorId(goods.getBioCabinetDoorId());
            bizEventReplaceService.clearBizEvent(event);
            bizEventMapper.insert(event);

            //【邮件】本次刷卡开门的卡主，柜门管理员
            bizEventReplaceService.sendEmailForUser(userId,event);
            bizEventReplaceService.sendEmailForDoorAdmin(event);
        }
    }

    /**
     * 添加记录-归还检查有不合格项
     *
     * @param ledgerId
     * @param goodsName
     * @param userName
     * @param qualifyNum
     * @param totalNum
     * @param userId
     * @param goods
     */
    private void addRecordForPrRetCheck(String ledgerId, String goodsName, String userName, int qualifyNum, int totalNum, String userId, BizGoods goods) {
        if (qualifyNum != totalNum) {
            //查询对应配置
            BizEventConfig eventConfig = bizEventConfigMapper.selectById("30");
            //事件描述
            String eventDesc = eventConfig.getEventDesc();
            eventDesc = eventDesc.replace("{{goods}}", goodsName);
            eventDesc = eventDesc.replace("{{borrower}}", userName);
            //合格率 合格数/总数
            String rate = NumberUtil.roundStr(NumberUtil.mul(NumberUtil.div(qualifyNum, totalNum), 100), 2);
            eventDesc = eventDesc.replace("{{pass-rate}}", rate + "%");
            //组合实体类
            BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
            event.setEventConfigId(eventConfig.getId());
            event.setEventTime(DateUtil.date());
            event.setEventDesc(eventDesc);
            event.setEventUserId(userId);
            event.setEventAction(8);
            event.setGoodsId(goods.getId());
            event.setGoodsLedgerId(ledgerId);
            event.setCabinetGroupId(goods.getBioCabinetGroupId());
            event.setCabinetId(goods.getBioCabinetId());
            event.setCabinetDoorId(goods.getBioCabinetDoorId());
            bizEventReplaceService.clearBizEvent(event);
            bizEventMapper.insert(event);

            //【邮件】本次刷卡开门的卡主，柜门管理员
            bizEventReplaceService.sendEmailForUser(userId,event);
            bizEventReplaceService.sendEmailForDoorAdmin(event);
        }
    }
}
